// SPDX-License-Identifier: GPL-2.0

#include "dim_core_init.h"
#include <linux/errno.h>
#include "dim_core_fs.h"
#include "dim_core_sig.h"
#include "dim_core_measure.h"
#include "dim_core_measure_log.h"
#include "dim_core_policy.h"
#include "dim_core_status.h"
#include "dim_core.h"
#include "dim_core_mem.h"
#include "../common/dim_hash.h"
#include "../adapter/dim_adapter.h"

int dim_init_env(void)
{
	int ret;

	dim_hash_set_cur_index("sha256");
	if (dim_set_extend_type(DIM_EXTEND_TYPE_TPM2) != 0)
		return -EINVAL;

	ret = dim_hash_algo_init();
	if (ret != 0)
		goto err;

	ret = dim_fs_init();
	if (ret != 0)
		goto err;

	return 0;
err:
	dim_reset_extend_type();
	dim_hash_algo_uninit();
	return ret;
}

int dim_init_baseline(void)
{
	int ret;

	ret = dim_mem_init();
	if (ret != 0)
		goto err;

	ret = dim_measure_log_init();
	if (ret != 0)
		goto err;

	return 0;
err:
	dim_measure_log_uninit();
	dim_mem_destroy();
	return ret;
}

int dim_init_timer(int interval)
{
	int ret;

	ret = dim_measure_timer_init();
	if (ret != 0)
		return ret;

	if (interval > 0)
		dim_measure_timer_set_intervel(interval);

	return 0;
}

void dim_uninit_timer(void)
{
	dim_measure_timer_destroy();
}

void dim_uninit_baseline(void)
{
	dim_measure_baseline_destroy();
	dim_measure_log_uninit();
	dim_mem_destroy();
	dim_sig_uninit();
}

void dim_uninit_env(void)
{
	dim_fs_destroy();
	dim_reset_extend_type();
	dim_hash_algo_uninit();
}
